package com.inspur.gs.sbssdl.sbwhgl.sbrh.sbrh.back.luCreatFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

public class SqlUtil {
    public static String getSql(String tableName, HashMap<String, Object> value){
        String prefix = "insert into " + tableName;

        StringBuilder columnSql = new StringBuilder("(");
        StringBuilder valueSql = new StringBuilder(" VALUES (");

        value.forEach((k,v) -> {
            columnSql.append(k).append(",");
            if (v instanceof LocalDateTime){
                String timeFormatter = ((LocalDateTime) v).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                valueSql.append(String.format("TIMESTAMP '%s'", timeFormatter)).append(",");
            }else {
                valueSql.append("'").append(v).append("'").append(",");
            }
        });

        columnSql.deleteCharAt(columnSql.length()-1)
                .append(")");
        valueSql.deleteCharAt(valueSql.length()-1)
                .append(")");

        return prefix + columnSql + valueSql;
    }
}
